<?php
/**
 * Created by VsCode.
 * User: Zhanghaibin
 * Date: 2018/3/29
 * Time: 16:14
 */

namespace Admin\Controller;


use Think\Controller\RestController;
/**
 * 
 * 设置关键字回复
 * basic ->文字回复
 * images->图片回复
 * news  ->图文回复
 * music ->音乐回复
 * video ->视频回复
 * system-> 设置首次关注/默认回复关键字
 * 
 */
class MessageReplyApiController extends RestController
{
    const REPLY_MOUDLE    = 'basic';

    public function basic_add(){
        $basicReplyId = '';
     
        D()->startTrans();
        try {   

            $ruleName = I('rule_name');
            $ruleKeywordsArr = I('rule_keywords');
            
            $basicReply = D('BasicReply');
    
            $keyworkds = explode(',',$ruleKeywordsArr) ;
            $data = array();
            if($basicReply->create()) {
                //插入基本信息表basic_reply
                $basicReplyId = $basicReply->add();
                $primaryKey = $basicReply->getLastInsID();
                $replyRuleData = array();
                if ($basicReplyId) {
                    $replyRule = D('ReplyRule');
                    foreach($keyworkds as $value) {
                        $replyRuleData['rule_name'] = $ruleName;
                        $replyRuleData['rule_keywords'] = $value;
                        $replyRuleData['moudle'] = self::REPLY_MOUDLE;
                        $replyRuleData['rule_id'] =  $primaryKey;
                        $replyRuleData['rule_status'] = 'ACTIVE';
                        $replyRuleData['createtime'] = time();
                        array_push($data,$replyRuleData);
                    }
                    $replyRule->addAll($data);
                }
            } 
            D()->commit();
            \Common\Common\print_json_data(200, '', '文字回复创建成功!');
        } catch (\Exception $e) {
            D()->rollback();
            \Common\Common\print_json_data(400, '', $e->getMessage());
           
        }
        
       exit;
    }

    
    public function basic_edit(){
        $basicReplyId = '';
     
        D()->startTrans();
        try {   

            $ruleId = I('rule_id');
            $ruleName = I('rule_name');
            $ruleKeywordsArr = I('rule_keywords');
            $basicReply = D('BasicReply');
            if (empty($ruleId)) {
                throw new \Exception('数据不存在!');
            }
            $replyRule = D('ReplyRule');
            $replyRule->where(array('rule_id'=>$ruleId))->delete();
            $keyworkds = explode(',',$ruleKeywordsArr) ;
            $data = array();
            if($basicReply->create()) {
                //插入基本信息表basic_reply
                $basicReplyId = $basicReply->save();
                $replyRuleData = array();
                if ($basicReplyId) {
                    $replyRule = D('ReplyRule');
                    foreach($keyworkds as $value) {
                        $replyRuleData['rule_name'] = $ruleName;
                        $replyRuleData['rule_keywords'] = $value;
                        $replyRuleData['moudle'] = self::REPLY_MOUDLE;
                        $replyRuleData['rule_id'] =  $ruleId;
                        $replyRuleData['rule_status'] = 'ACTIVE';
                        $replyRuleData['createtime'] = time();
                        array_push($data,$replyRuleData);
                    }
                    $replyRule->addAll($data);
                }
            } 
            D()->commit();
            \Common\Common\print_json_data(200, '', '文字回复创建成功!');
        } catch (\Exception $e) {
            D()->rollback();
            \Common\Common\print_json_data(400, '', $e->getMessage());
           
        }
        
       exit;
    }

    
    public function basic_list(){
     
        try {   

            $ruleName = I('rule_name');
            $basicReply = D('BasicReply');
            $replyRule  = D('ReplyRule');
            $conditions['moudle'] = self::REPLY_MOUDLE;
            if ($ruleName) {
                $conditions['rule_name'] = array("like","%".$ruleName."%");
            }
            $rulesArray = array();
            $returnData = array();
            $finalData = array();
            if (IS_POST) {
                // 查找符合规则的rule_id
                $ruleIds = $replyRule->field('rule_id')
                                     ->where($conditions)
                                     ->group('rule_id')
                                     ->page(I('post.page'), 15)
                                     ->select();
                $returnData['max_page'] = ceil($replyRule->field('rule_id')
                                                ->where($conditions)
                                                ->group('rule_id')
                                                ->count()/15);
                $ruleIdParams = array();
                foreach($ruleIds as $value) {
                    array_push($ruleIdParams,$value['rule_id']);
                }

                //$rulesArray =$ruleIdParams;
                //查找文字回复
                if (!empty($ruleIdParams)) {
                    $basicCondition['rule_id'] = array('IN',$ruleIdParams);
                    $conditions['rule_id'] = array('IN',$ruleIdParams);
                }
                $basicReplyResult = $basicReply->where($basicCondition)->select();
                //查找规则列表
                $ruleResult = $replyRule->where($conditions)
                                        ->select();
                foreach ($ruleResult as $value) {
                    //存在
                    
                    if (isset($rulesArray[$value['rule_id']])) {
                        $tmpArray = array();
                        //$ddd = $rulesArray[$value['rule_id']]['rule_keywords'];
                        $tmpArray =explode(',',$rulesArray[$value['rule_id']]['rule_keywords']);// explode(',',$rulesArray[$value['rule_id']]['rule_keywords']);
                        array_push($tmpArray,$value['rule_keywords']);
                        $rulesArray[$value['rule_id']]['rule_keywords'] =implode(',',$tmpArray);
                    } else {
                        $data=array();
                        $data['rule_name'] = $value['rule_name'];
                        $data['rule_keywords'] = $value['rule_keywords'];
                        $data['rule_status'] = $value['rule_status'];
                        $rulesArray[$value['rule_id']] = $data;
                    }
                }
                foreach($basicReplyResult as $value) {
                    $value['rule'] = $rulesArray[$value['rule_id']];
                    array_push($finalData,$value);
                }
                $returnData['records'] = $finalData;
            }

    
            \Common\Common\print_json_data(200, $returnData, '文字回复创建成功!');
        } catch (\Exception $e) {
            \Common\Common\print_json_data(400, '', $e->getMessage());
           
        }
        
       exit;
    }

    public function basic_remove() {

        if(IS_POST) {
            $ruleId = I('rule_id');

            $condition['rule_id'] = $ruleId;
            $basicReply = D('BasicReply');
            $replyRule  = D('ReplyRule');
            $basicReply->where($condition)->delete();
            $replyRule->where($condition)->delete();

            \Common\Common\print_json_data(200, '', '删除成功');

        } else {
            \Common\Common\print_json_data(400, '', '请求方式错误');
        }
    }


    
    
    public function basic_get(){
     
        try {   

            $ruleId = I('rule_id');
            $basicReply = D('BasicReply');
            $replyRule  = D('ReplyRule');
            $conditions['moudle'] = self::REPLY_MOUDLE;
            $rulesArray = array();
            $returnData = array();
            $finalData = array();
            if (IS_GET) {

                //$rulesArray =$ruleIdParams;
                //查找文字回复
                $basicCondition['rule_id'] = $ruleId;
                $basicReplyResult = $basicReply->where($basicCondition)->find();
                //查找规则列表
                $conditions['rule_id'] = $ruleId;
                $ruleResult = $replyRule->where($conditions)
                                        ->select();
                foreach ($ruleResult as $value) {
                    //存在
                    
                    if (isset($rulesArray[$value['rule_id']])) {
                        $tmpArray = array();
                        //$ddd = $rulesArray[$value['rule_id']]['rule_keywords'];
                        $tmpArray =explode(',',$rulesArray[$value['rule_id']]['rule_keywords']);// explode(',',$rulesArray[$value['rule_id']]['rule_keywords']);
                        array_push($tmpArray,$value['rule_keywords']);
                        $rulesArray[$value['rule_id']]['rule_keywords'] =implode(',',$tmpArray);
                    } else {
                        $data=array();
                        $data['rule_name'] = $value['rule_name'];
                        $data['rule_keywords'] = $value['rule_keywords'];
                        $data['rule_status'] = $value['rule_status'];
                        $rulesArray[$value['rule_id']] = $data;
                    }
                }
                $basicReplyResult['rule'] = $rulesArray[ $ruleId];
                $finalData = $basicReplyResult;
                $returnData['records'] = $finalData;
            }

    
            \Common\Common\print_json_data(200, $returnData, '文字回复创建成功!');
        } catch (\Exception $e) {
            \Common\Common\print_json_data(400, '', $e->getMessage());
           
        }
        
       exit;
    }

}   